<template>
  <a-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="钢号">
              <a-input placeholder="请输入钢号" v-model="queryParam.steelSpecification"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="规格">
              <a-input placeholder="请输入规格" v-model="queryParam.standard"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="正常/取消">
              <a-radio-group :default-value="-1" v-model="queryParam.isEnable">
                <a-radio :value="-1">
                  全部
                </a-radio>
                <a-radio :value="1">
                  正常
                </a-radio>
                <a-radio :value="0">
                  取消
                </a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('构件表')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel">
          <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl1"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入原有系统的数据</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{x:true}"
        :columns="columns"
        class="table-color-dust"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="qrImg" slot-scope="text">
          <qrcode :value=text :options="{ size: 20 }"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <template slot="flow" slot-scope="text, record">
          <div v-if="record.flowName !== null">{{record.flowName}}</div>
          <div v-if="record.flowName === null">
            <editable-cell
              formType="select"
              :defaultEditable="true"
              :required="true"
              :autoSave="true"
              :decoratorOptions="rules.flow"
              message="请选择工艺线路"
              :value="record.flowId ? null : '1'"
              :text="record.flowName"
              @selectChange="onCellChange(record,record.id,$event)">
              <a-select-option v-for = "flow in flowList" :key="flow.id" :value="flow.id">{{flow.flowName}}</a-select-option>
            </editable-cell>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a @click="mprint(record.id)">打印二维码</a>
        </span>

        <template slot="footer" slot-scope="currentPageData">
          <span style="font-weight: bold">合计：</span>&emsp;&emsp;&emsp;&emsp;
          <span>总重量(kg)：{{ totalWeight }}</span>&emsp;&emsp;&emsp;&emsp;
          <span>总面积(㎡)：{{ totalArea }}</span>&emsp;&emsp;&emsp;&emsp;
          <span>总数量：{{ totalCount }}</span>
        </template>

        <span slot="isOpen" slot-scope="text, record">
          <a-popconfirm v-if="text != 1" title="确认要启用吗?" @confirm="priceConfirm(record)">
            <a-switch v-model="record.isEnable === 1" :loading="switchLoading"/>
          </a-popconfirm>
          <a-popconfirm v-else title="确认要禁用吗?" @confirm="priceConfirm(record)">
            <a-switch v-model="record.isEnable === 1" :loading="switchLoading"/>
          </a-popconfirm>
        </span>
      </a-table>
    </div>

    <frame-modal ref="modalForm" @ok="modalFormOk" :mainId="mainId"></frame-modal>
    <FramePrint ref="framePrint"></FramePrint>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import FrameModal from './modules/FrameModal';
  import FramePrint from "./modules/FramePrint";

  import EditableCell from "@/components/EditableCell/cell.vue";
  import { getAction,putAction } from '@/api/manage'

  import QrcodeVue from 'qrcode.vue'

  export default {
    name: "FrameList",
    mixins:[JeecgListMixin],
    components: { FrameModal,FramePrint,qrcode:QrcodeVue,EditableCell},
    props:{
      mainId:{
        type:String,
        default:'',
        required:false
      }
    },
    watch:{
      mainId:{
        immediate: true,
        handler(val) {
          if(!this.mainId){
            this.clearList()
          }else{
            this.queryParam['engineeringId'] = val
            this.loadData(1);
          }
        }
      }
    },
    data () {
      getAction("/flow/flow/selectAll").then(res=>{
        if(res.success){
          this.flowList = res.result;
        }
      });
      return {
        description: '工程表管理页面',
        disableMixinCreated:true,
        test:'0ppp',
        // 表头
        columns: [
          {
            title:'钢号',
            align:"center",
            dataIndex: 'steelSpecification'
          },
          {
            title:'规格',
            align:"center",
            dataIndex: 'standard'
          },
          {
            title:'材质',
            align:"center",
            dataIndex: 'qualit'
          },
          {
            title:'图号',
            align:"center",
            dataIndex: 'figureNumber'
          },
          {
            title:'版次',
            align:"center",
            dataIndex: 'version'
          },
          {
            title:'位置',
            align:"center",
            dataIndex: 'location'
          },
          {
            title:'单个重量(kg)',
            align:"center",
            dataIndex: 'singleWeight'
          },
          {
            title:'总重(kg)',
            align:"center",
            dataIndex: 'totalWeight'
          },
          {
            title:'数量',
            align:"center",
            dataIndex: 'count'
          },
          {
            title:'面积',
            align:"center",
            dataIndex: 'area'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title:'工艺路线',
            align:"center",
            dataIndex: 'flowId',
            width: 120,
            scopedSlots: { customRender: "flow" }
          },
          {
            title:'二维码',
            align:"center",
            dataIndex: 'id',
            width: 150,
            scopedSlots: { customRender: 'qrImg' }
          },
          {
            title: '正常/取消',
            dataIndex: 'isEnable',
            align:"center",
            fixed:"right",
            width:80,
            scopedSlots: { customRender: 'isOpen' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:180,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/engine/engineering/listFrameByMainId",
          delete: "/engine/engineering/deleteFrame",
          deleteBatch: "/engine/engineering/deleteBatchFrame",
          exportXlsUrl: "/engine/engineering/exportFrame",
          importUrl: "/engine/engineering/importFrame",
          importUrl1: "/excel/import/addFrame",
        },
        dictOptions:{
        },
        flowList: [],
        totalWeight: 0,
        totalArea: 0,
        totalCount: 0,
        switchLoading: false,
        rules: {
          name: {
            rules: [
              {
                required: true,
                message: "请输入姓名"
              }
            ]
          },
          flow: {
            rules: [
              {
                required: true,
                message: "请输入姓名"
              }
            ]
          }
        }
      }
    },
    computed: {
      importExcelUrl(){
        return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`;
      },
      importExcelUrl1(){
        return `${window._CONFIG['domianURL']}/${this.url.importUrl1}/${this.mainId}`;
      }
    },
    methods: {
      clearList(){
        this.dataSource=[]
        this.selectedRowKeys=[]
        this.ipagination.current = 1
      },
      mprint: function (id) {
        this.$refs.framePrint.show(id);
      },
      onCellChange: function (record,id,value) {
        let params = {};
        params.id = id;
        params.flowId = value.key;
        params.flowName = value.label;
        putAction("/frame/addTask",params).then((res) => {
          if(res.success){
            record.flowId = value.key;
            record.flowName = value.label;
          }
        });
      },
      setdataCss(record) {
        let className = 'table-color-dust';
        if (record.flowId===null || record.flowId ==='' || record.flowId ==='1'){
        }
        return className;
      },
      loadData(arg){
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
            this.totalWeight = res.totalWeight;
            this.totalCount = res.totalCount;
            this.totalArea = res.totalArea;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      // 确认/取消确认
      priceConfirm(record) {
        this.switchLoading = true;
        if (record.isEnable === 1){
          record.isEnable  = 0;
        }else {
          record.isEnable  = 1;
        }
        putAction('/frame/openFrame', record).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
          } else this.$message.warning(res.message);
          this.switchLoading = false;
        })
      },
      searchReset(){
        this.queryParam.isEnable=-1
        this.queryParam.steelSpecification = "";
        this.queryParam.standard = "";
        this.loadData(this.ipagination)
      }
    }
  };
</script>
<style scoped>
  @import '~@assets/less/common.less'
  .table-color-dust {
    background-color: #F5222D;
  }
</style>
